package com.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.health.constant.MessageConstant;
import com.health.dao.PermissionDao;
import com.health.entity.PageResult;
import com.health.entity.QueryPageBean;
import com.health.entity.Result;
import com.health.pojo.Permission;
import com.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Classname PermissionServiceImpl
 * @Description TODO
 * @Version 1.0.0
 * @Date 2023/3/18 16:26
 * @Author RainGrd
 */
@Service(interfaceClass = PermissionService.class)
@Transactional
@Slf4j
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    @Override
    public PageResult queryPermissionPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //查询角色
        List<Permission> permissions = permissionDao.selectPermissionsByCondition(queryPageBean.getQueryString());
        log.info("users:{}", permissions);
        //遍历角色
        for (Permission permission : permissions) {
            permission.setRoles(permissionDao.selectRolesByPermissionId(permission.getId()));
        }

        return new PageResult(permissionDao.selectTotalByPermissionId(queryPageBean.getQueryString()), permissions);
    }

    @Override
    public Result savePermission(Permission permission, Integer[] roleIds) {
        log.info("permission:{}", permission);
        try {
            int i = permissionDao.insertPermission(permission);
            log.info("permission:{}", permission);
            //插入菜单与角色表
            setPermissionIdAndRoleIds(permission.getId(), roleIds);
            return i > 0 ? Result.success(MessageConstant.ADD_CHECKITEM_SUCCESS) : Result.error(MessageConstant.ADD_CHECKITEM_FAIL);
        } catch (Exception e) {
            return Result.error(MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    @Override
    public Result editPermission(Permission permission, Integer[] roleIds) {
        try {
            //根据检查组id删除中间表数据（清理原有关联关系）
            permissionDao.deleteAssociation(permission.getId());
            //重新补充中间表
            setPermissionIdAndRoleIds(permission.getId(), roleIds);

            //正式编辑用户
            int i = permissionDao.updatePermission(permission);
            return i > 0 ? Result.success(MessageConstant.EDIT_CHECKGROUP_SUCCESS) : Result.error(MessageConstant.EDIT_CHECKGROUP_FAIL);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error();
        }
    }

    @Override
    public List<Integer> queryRoleIdsByPermissionId(Integer permissionId) {
        return permissionDao.selectRoleIdsByPermissionId(permissionId);
    }

    @Override
    public Result clearPermissionId(Integer permissionId) {
        //删除检查组
        try {
            //根据检查组id删除中间表数据（清理原有关联关系）
            permissionDao.deleteAssociation(permissionId);
            //删除检查组
            int i = permissionDao.deletePermission(permissionId);
            return i > 0 ? Result.success(MessageConstant.DELETE_CHECKGROUP_SUCCESS) : Result.error(MessageConstant.DELETE_CHECKGROUP_FAIL);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error();
        }
    }

    /**
     * 用于设置套餐与检查组的关系
     *
     * @param roleIds
     * @param permission
     */
    private void setPermissionIdAndRoleIds(Integer permission, Integer[] roleIds) {
        //判断检查项目Id数组是否为空
        if (roleIds != null && roleIds.length > 0) {
            //添加检查组和检查项
            for (Integer roleId : roleIds) {
                log.info("roleId:{}", roleId);
                permissionDao.insertPermissionAndRole(permission, roleId);
            }
        }
    }
}
